﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Mrt.BoardGameUp.Model;

namespace MvcApp.Controllers
{
    public class GamingSessionController : Controller
    {
        private BoardGameUpEntities db = new BoardGameUpEntities();

        //
        // GET: /GamingSession/

        public ActionResult Index()
        {
            var gamingsession = db.GamingSession.Include(g => g.BoardGame).Include(g => g.User);
            return View(gamingsession.ToList());
        }

        //
        // GET: /GamingSession/Details/5

        public ActionResult Details(int id = 0)
        {
            GamingSession gamingsession = db.GamingSession.Find(id);
            if (gamingsession == null)
            {
                return HttpNotFound();
            }
            return View(gamingsession);
        }

        //
        // GET: /GamingSession/Create

        public ActionResult Create()
        {
            ViewBag.BoardGameId = new SelectList(db.BoardGame, "BoardGameId", "Name");
            ViewBag.UserId = new SelectList(db.User, "UserId", "Name");
            return View();
        }

        //
        // POST: /GamingSession/Create

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(GamingSession gamingsession)
        {
            if (ModelState.IsValid)
            {
                db.GamingSession.Add(gamingsession);
                db.SaveChanges();
                return RedirectToAction("Index");
            }

            ViewBag.BoardGameId = new SelectList(db.BoardGame, "BoardGameId", "Name", gamingsession.BoardGameId);
            ViewBag.UserId = new SelectList(db.User, "UserId", "Name", gamingsession.UserId);
            return View(gamingsession);
        }

        //
        // GET: /GamingSession/Edit/5

        public ActionResult Edit(int id = 0)
        {
            GamingSession gamingsession = db.GamingSession.Find(id);
            if (gamingsession == null)
            {
                return HttpNotFound();
            }
            ViewBag.BoardGameId = new SelectList(db.BoardGame, "BoardGameId", "Name", gamingsession.BoardGameId);
            ViewBag.UserId = new SelectList(db.User, "UserId", "Name", gamingsession.UserId);
            return View(gamingsession);
        }

        //
        // POST: /GamingSession/Edit/5

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(GamingSession gamingsession)
        {
            if (ModelState.IsValid)
            {
                db.Entry(gamingsession).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.BoardGameId = new SelectList(db.BoardGame, "BoardGameId", "Name", gamingsession.BoardGameId);
            ViewBag.UserId = new SelectList(db.User, "UserId", "Name", gamingsession.UserId);
            return View(gamingsession);
        }

        //
        // GET: /GamingSession/Delete/5

        public ActionResult Delete(int id = 0)
        {
            GamingSession gamingsession = db.GamingSession.Find(id);
            if (gamingsession == null)
            {
                return HttpNotFound();
            }
            return View(gamingsession);
        }

        //
        // POST: /GamingSession/Delete/5

        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            GamingSession gamingsession = db.GamingSession.Find(id);
            db.GamingSession.Remove(gamingsession);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}